﻿using System.Collections.Generic;
using System.ComponentModel;
using NHibernate.Collection.Generic;
using NHibernate.Engine;

namespace hbse.Persistence
{
    public class PersistentBindingBag<T> : PersistentGenericBag<T>, IBindingList
    {
        public PersistentBindingBag() { }
        public PersistentBindingBag(ISessionImplementor session) : base(session) { }
        public PersistentBindingBag(ISessionImplementor session, ICollection<T> coll)
            : base(session)
        {
            IList<T> bl = coll as BindingList<T>;
            if (bl == null)
            {
                bl = coll as IList<T>;
                bl = new BindingList<T>(bl ?? new List<T>(coll)) { AllowNew = true };
            }

            InternalBag = bl;
            SetInitialized();
            IsDirectlyAccessible = true;
        }

        public object AddNew()
        {
            return ((IBindingList)InternalBag).AddNew();
        }

        public void AddIndex(PropertyDescriptor property)
        {
            ((IBindingList)InternalBag).AddIndex(property);
        }

        public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
        {
            ((IBindingList)InternalBag).ApplySort(property, direction);
        }

        public int Find(PropertyDescriptor property, object key)
        {
            return ((IBindingList)InternalBag).Find(property, key);
        }

        public void RemoveIndex(PropertyDescriptor property)
        {
            ((IBindingList)InternalBag).RemoveIndex(property);
        }

        public void RemoveSort()
        {
            ((IBindingList)InternalBag).RemoveSort();
        }

        public bool AllowNew
        {
            get { return ((IBindingList)InternalBag).AllowNew; }
        }

        public bool AllowEdit
        {
            get { return ((IBindingList)InternalBag).AllowEdit; }
        }

        public bool AllowRemove
        {
            get { return ((IBindingList)InternalBag).AllowRemove; }
        }

        public bool SupportsChangeNotification
        {
            get { return ((IBindingList)InternalBag).SupportsChangeNotification; }
        }

        public bool SupportsSearching
        {
            get { return ((IBindingList)InternalBag).SupportsSearching; }
        }

        public bool SupportsSorting
        {
            get { return ((IBindingList)InternalBag).SupportsSorting; }
        }

        public bool IsSorted
        {
            get { return ((IBindingList)InternalBag).IsSorted; }
        }

        public PropertyDescriptor SortProperty
        {
            get { return ((IBindingList)InternalBag).SortProperty; }
        }

        public ListSortDirection SortDirection
        {
            get { return ((IBindingList)InternalBag).SortDirection; }
        }

        public event ListChangedEventHandler ListChanged
        {
            add
            {
                ((IBindingList)InternalBag).ListChanged += value;
            }
            remove
            {
                ((IBindingList)InternalBag).ListChanged -= value;
            }
        }

        
    }
}
